package com.gwc;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = {"/sessionA"})
public class SessionA extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        // 获取session对象
        HttpSession session = req.getSession();
        String sessionId = session.getId();
        System.out.println(sessionId);
        // 会判断该session是否存在，若不存在，则会新建一个session
        System.out.println(session.isNew());
        // 向session中写入数据
        // session的时效性需要去配置文件中进行配置
        session.setAttribute("username",username);

    }
}
